#! /usr/bin/python2.4

# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""block_some_prop_mods.py unittest"""


import unittest

import svn.fs
import svn.repos

import gvn.hooks.info
import testing.hooks.common

# import RunHook to pass the function pointer to testcommon in unittests
from gvn.hooks.pre_commit.block_some_prop_mods import RunHook



class TestCase(testing.hooks.common.HookTestCase):

  def setUp(self):
    # HookTestCase creates and sets a _logger object based on whether
    # GVN_TEST_DEBUG is exported or not
    testing.hooks.common.HookTestCase.setUp(self)

    # create group pickle to use as our group file
    user_groups= {
      "polly": [ "group1" ],
    }

    for user in user_groups:
      self.userdb.WriteUserGroups(user, user_groups[user])

  def testRandomRootProp(self):
    """Check setting random prop at the root.
    """
    self.assertHookResult(self._TestProp(RunHook, "proptest", "propvalue"), 0)

  def testBadNameNodeProp(self):
    """Check setting a node property with a bad name
    """
    self.assertHookResult(self._TestProp(RunHook, " ", "propvalue"),
                          "Rejecting new property on / with forbidden name.*")

  def testBadNameNodeProp2(self):
    """Check setting a node property with a bad name2
    """
    self.assertHookResult(self._TestProp(RunHook, "\n", "propvalue"),
                          "Rejecting new property on / with forbidden name.*")

  def testBadNameRevProp(self):
    """Check setting a rev property with a bad name
    """
    self.assertHookResult(self._TestProp(RunHook, " ", "propval", path="REV"),
                          "Rejecting rev property with forbidden name.*")

  def testBadNameRevProp2(self):
    """Check setting a rev property with a bad name2
    """
    self.assertHookResult(self._TestProp(RunHook, "\n", "propval", path="REV"),
                          "Rejecting rev property with forbidden name.*")

  def testModifyBadNameRevProp(self):
    """Allow modifying a prop that would otherwise be denied.
    """
    # First, create a badly named revprop
    pool = None
    repo = svn.repos.open(self.repo_name, pool)
    fs = svn.repos.fs(repo)
    txn = testing.hooks.common.BuildPropTxn(fs, prop_name=" ",
                                          prop_val="basil", path="REV",
                                          author="basil", logmsg="", pool=pool)
    # If there is a conflict or other commit problem, it'll create an exception.
    # Don't trust, or rely on the conflicts variable as the python svn interface
    # doesn't seem to be able to pass on data to it.
    (conflicts, revision) = svn.fs.commit_txn(txn, pool)

    # Now try the otherwise disallowed commit
    self.assertHookResult(self._TestProp(RunHook, " ", "val", path="REV"), 0)


  def testGvnRootProp(self):
    """Check setting gvn:x prop at the root.
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:x", "farty towels"), 0)

  def testAddGvnSuperUsersRootProp(self):
    """Do not allow anyone to add gvn:superusers at the root.
       This needs to be done before the hook is installed.
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:superusers",
                          "group: fowls"),
                          "user polly cannot modify gvn:superusers")

  def testModifyGvnSuperUsersRootProp(self):
    """Forbid Modifying gvn:superusers at the root.
    """
    # First, set a gvn:superusers on the root that does not include polly.
    pool = None
    repo = svn.repos.open(self.repo_name, pool)
    fs = svn.repos.fs(repo)
    txn = testing.hooks.common.BuildPropTxn(fs, prop_name="gvn:superusers",
                                          prop_val="basil", path="",
                                          author="basil", logmsg="", pool=pool)
    # If there is a conflict or other commit problem, it'll create an exception.
    # Don't trust, or rely on the conflicts variable as the python svn interface
    # doesn't seem to be able to pass on data to it.
    (conflicts, revision) = svn.fs.commit_txn(txn, pool)

    # Now try to set gvn:superusers on the repository root.
    self.assertHookResult(self._TestProp(RunHook, "gvn:superusers",
                          "group: fowls"),
                          ("user polly cannot modify gvn:superusers property "
                           "on repository root"))

  def testGvnSuperUsersSubdirProp(self):
    """Check setting gvn:superusers at public/projects.
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:superusers",
                          "group: fowls",
                          path="public/projects"), 0)

  def testGvnRootPropWithSuperusersSet(self):
    """Check that mortal users can edit non-gvn:superusers properties
    on the root even when gvn:superusers is set (we used to have a bug
    where the check blocked all repo root changes when gvn:superusers
    was set, whether the user was changing gvn:superusers or not).
    """
    pool = None
    repo = svn.repos.open(self.repo_name, pool)
    fs = svn.repos.fs(repo)
    txn = testing.hooks.common.BuildPropTxn(fs, prop_name="gvn:superusers",
                                          prop_val="basil", path="",
                                          author="basil", logmsg="", pool=pool)

    # If there is a conflict or other commit problem, it'll create an exception.
    # Don't trust, or rely on the conflicts variable as the python svn interface
    # doesn't seem to be able to pass on data to it.
    (conflicts, revision) = svn.fs.commit_txn(txn, pool)

    # Now try to set gvn:project on the repository root.
    self.assertHookResult(self._TestProp(RunHook, "gvn:project", "foo"), 0)

if __name__ == "__main__":
  unittest.main()
